<?php

namespace App\Http\Controllers\Mobile;

use App\Mall\OrderCreator;
use App\Models\Cart;
use App\Models\Item;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class AuctionController extends Controller
{
    //
    public function buynow()
    {
        $itemid = intval($this->request->input('itemid'));
        $quantity = intval($this->request->input('quantity'));

        $item = Item::find($itemid);
        if ($item) {
            $total_fee = floatval($item->price) * $quantity;

            $shipping = Auth::user()->addresses()->orderByDesc('isdefault')->first();
            if (!$shipping) {
                return redirect('mobile/address');
            }

            return $this->view('mobile.auction.buynow', compact('itemid', 'quantity', 'item', 'total_fee', 'shipping'));
        } else {
            return false;
        }
    }

    public function createOrder()
    {
        $itemid = $this->request->input('itemid');
        $item = Item::find($itemid);

        if ($item) {
            $item->quantity = intval($this->request->input('quantity'));
            $creator = new OrderCreator();
            $creator->shop         = $item->shop;
            $creator->items        = [$item];
            $creator->payType      = $this->request->input('pay_type');
            $creator->shippingType = $this->request->input('shipping_type');
            $creator->addressId = $this->request->input('address_id');

            try{
                $order = $creator->createOrder();
                if ($creator->payType == 1) {
                    return redirect('/mobile/trade/detail?order_id='.$order->order_id);
                } else {
                    return redirect('/mobile/trade/bought');
                }
            }catch (\Exception $e) {
                return $this->showError($e->getMessage());
            }
        } else {
            return $this->showError(trans('mall.item_not_exists'));
        }
    }

    public function getOrderInfo()
    {
        $total_fee = 0;
        $shops = Cart::with(['items'=>function($query){
            return $query->where('uid', $this->uid);
        }])->where('uid', $this->uid)->groupBy('shop_id')
            ->get(['id', 'uid', 'shop_id', 'shop_name'])
            ->map(function ($shop) use (&$total_fee){
                $shop->items = $shop->items->map(function ($item) use (&$total_fee){
                    $item->thumb = image_url($item->thumb);
                    $item->image = image_url($item->image);
                    $total_fee+= $item->price * $item->quantity;
                    return $item;
                });
                return $shop;
            });

        $shipping = Auth::user()->addresses()->orderByDesc('isdefault')->first();
        if (!$shipping) {
            return redirect('mobile/address');
        }

        $total_fee = formatAmount($total_fee);
        return $this->view('mobile.auction.getorderinfo', compact('shops', 'total_fee', 'shipping'));
    }

    public function settlement()
    {
        $address_id = $this->request->input('address_id');
        $buyer_message = $this->request->input('buyer_message');
        $items = $this->request->input('items');

        $shops = Cart::with(['items'=>function($query){
            return $query->where('uid', $this->uid);
        }])->where('uid', $this->uid)->whereIn('itemid', $items)->groupBy('shop_id')->get(['shop_id','shop_name']);

        foreach ($shops as $shop) {
            $creator = new OrderCreator();
            $creator->shop  = $shop->shop;
            $creator->items = $shop->items;
            $creator->addressId = $address_id;
            $creator->payType = 1;
            $creator->shippingType = 1;
            $creator->buyerMessage = $buyer_message[$shop->shop_id];

            try {
                $creator->createOrder();
            } catch (\Exception $e){

            }
        }
        //清空购物车
        Auth::user()->cartItems()->whereIn('itemid', $items)->delete();
        return redirect('mobile/trade/bought');
    }
}
